package cn.hn.yh.controller.user;

import cn.hn.yh.vo.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/user")
//@PropertySource("classpath:application.yml")
public class LoginController {
    @Autowired
    private Environment environment;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(User user, Model model) {
//        Subject subject = SecurityUtils.getSubject();
//        subject.login(new UsernamePasswordToken(user.getUserename(), user.getPassword()));

        String username = environment.getProperty("user.username");
        String password = environment.getProperty("user.password");
        System.out.println(user.getUsername());
        System.out.println(user.getPassword());
        System.out.println(username);
        System.out.println(password);
        if (user.getUsername() != null && !username.equals("")) {
            if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
                model.addAttribute("msg", "登陆成功");
                System.out.println("有人访问后端服务器成功");
                return "success/success";
            } else {
                model.addAttribute("msg", "用户名或密码错误");
                System.out.println("有人访问后端服务器失败1");
                return "index";
            }
        } else {
            model.addAttribute("msg", "用户名不存在");
            System.out.println("有人访问后端服务器失败2");
            return "index";
        }
    }
//    @RequestMapping(value = "/login", method = RequestMethod.POST)
//    public String login(String username, String password, Model model) {
////        Subject subject = SecurityUtils.getSubject();
////        subject.login(new UsernamePasswordToken(user.getUserename(), user.getPassword()));
//
//        String username2 = environment.getProperty("user.username");
//        String password2 = environment.getProperty("user.password");
//        System.out.println(username);
//        System.out.println(password);
//        if (username != null && !username.equals("")) {
//            if (username.equals(username2) && password.equals(password)) {
//                model.addAttribute("msg", "登陆成功");
//                System.out.println("有人访问后端服务器成功");
//                return "success/success";
//            } else {
//                model.addAttribute("msg", "用户名或密码错误");
//                System.out.println("有人访问后端服务器失败1");
//                return "index";
//            }
//        } else {
//            model.addAttribute("msg", "用户名不存在");
//            System.out.println("有人访问后端服务器失败2");
//            return "index";
//        }
//    }
}
